/**
 * @file 告警相关类型定义
 * @description 定义告警规则、告警动作和告警记录等类型
 * @date 2024-06-18
 */

import { BaseEntity, Timestamp } from './common';
import { GeoCoordinate, LocationWithAddress } from './map';

/**
 * 告警级别枚举
 */
export enum AlertLevel {
  INFO = 'info',
  WARNING = 'warning',
  ERROR = 'error',
  CRITICAL = 'critical'
}

/**
 * 告警规则状态枚举
 */
export enum AlertRuleStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive'
}

/**
 * 告警规则类型枚举
 */
export enum AlertRuleType {
  GEO_FENCE = 'geo-fence',
  SPEED = 'speed',
  OFFLINE = 'offline',
  CUSTOM = 'custom'
}

/**
 * 告警动作类型枚举
 */
export enum AlertActionType {
  NOTIFICATION = 'notification',
  EMAIL = 'email',
  SMS = 'sms',
  WEBHOOK = 'webhook'
}

/**
 * 告警记录状态枚举
 */
export enum AlertRecordStatus {
  NEW = 'new',
  ACKNOWLEDGED = 'acknowledged',
  RESOLVED = 'resolved'
}

/**
 * 告警条件基础接口
 */
export interface AlertConditionBase {
  type: AlertRuleType;
}

/**
 * 地理围栏告警条件
 */
export interface GeoFenceCondition extends AlertConditionBase {
  type: AlertRuleType.GEO_FENCE;
  boundary: GeoCoordinate[];
  mode: 'in' | 'out'; // 进入围栏或离开围栏触发
  name?: string;
}

/**
 * 速度告警条件
 */
export interface SpeedCondition extends AlertConditionBase {
  type: AlertRuleType.SPEED;
  threshold: number;
  operator: '>' | '<' | '>='; // 大于、小于或等于阈值触发
  duration?: number; // 持续时间（秒）
}

/**
 * 离线告警条件
 */
export interface OfflineCondition extends AlertConditionBase {
  type: AlertRuleType.OFFLINE;
  duration: number; // 离线持续时间（秒）
}

/**
 * 自定义告警条件
 */
export interface CustomCondition extends AlertConditionBase {
  type: AlertRuleType.CUSTOM;
  expression: string; // 自定义表达式
  params?: Record<string, any>; // 表达式参数
}

/**
 * 告警条件判别联合类型
 */
export type AlertCondition = 
  | GeoFenceCondition
  | SpeedCondition
  | OfflineCondition
  | CustomCondition;

/**
 * 告警规则类型
 */
export interface AlertRule extends BaseEntity {
  name: string;
  deviceId?: string; // 如果是针对特定设备的规则
  type: AlertRuleType; // 规则类型
  condition: AlertCondition; // 告警条件
  level: AlertLevel; // 告警级别
  status: AlertRuleStatus; // 规则状态
  actions?: AlertAction[]; // 告警触发的动作
  description?: string;
}

/**
 * 告警动作类型
 */
export interface AlertAction extends BaseEntity {
  type: AlertActionType; // 动作类型
  target: string; // 目标（如邮箱、手机号等）
  content?: string; // 自定义内容
}

/**
 * 告警记录位置信息
 */
export type AlertLocation = LocationWithAddress;

/**
 * 告警记录类型
 */
export interface AlertRecord extends BaseEntity {
  ruleId: string;
  deviceId: string;
  timestamp: Timestamp;
  level: AlertLevel;
  message: string;
  location?: AlertLocation;
  status: AlertRecordStatus;
  handledBy?: string;
  handleTime?: Timestamp;
  handleComment?: string;
} 